MACHINE LEARNING by Callaway Jason

MACHINE LEARNING by Callaway Jason

Author:Callaway, Jason
Language: eng
Format: epub, pdf
Publisher: UNKNOWN
Published: 2021-10-16T16:00:00+00:00


Figure 6: Clusters assigned to the instances

We can plot the three clusters (0, 1, and 2) on the same scatter plot using the code below:

plt.figure(figsize=(16, 10))markers = ['x', '.', '+']

for i in range(0, 3): cond = df['Cluster'] == i df2 = df[cond] plt.scatter(df2['X1'], df2['X2'], label = 'Cluster '+str(i), marker = markers[i])

plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], color = 'black', s = 50, marker = "s")

plt.xlabel('X1')plt.ylabel('X2')plt.legend()

Here, we first specify the size of our chart and declare a list called markers to store the markers for our clusters. Next, we use a for loop (that runs 3 times) to plot the clusters.

The first time the loop runs, i equals 0. The condition df['Cluster'] == i becomes df['Cluster'] == 0.

This condition selects rows in df with a Cluster value of 0. We assign the selected rows to a new DataFrame called df2 and use the scatter() function to plot a scatter plot for these rows, passing label = 'Cluster 0' and marker = 'x' to the function.

This plots all the data points in df with a Cluster value of 0, using 'x' as the marker.

After plotting the points for the first cluster, the for loop runs two more times to plot the other two clusters, changing the label and marker for each plot.

After plotting all three clusters, the for loop ends. Outside the for loop, we use the cluster_centers_ attribute to plot the centroids of our clusters.

cluster_centers_ stores the centroids as a NumPy array.

cluster_centers_[:, 0] gives us the first column of this array (i.e., the x-values for the centroids), while cluster_centers_[:, 1] gives us the second (i.e., the y-values).

If you run the code above, you’ll get the ff. chart:

Figure 7: Scatter plot showing the different clusters and centroids

Clusters 0, 1, and 2 are plotted using 'x', '.', and '+', respectively, while the centroids are plotted using squares.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.